﻿using System;
using System.Collections.Generic;
using System.Collections;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Expression;

namespace Model {
  
  public class Cliente {

    private string idCliente;
    private String nome;
    private String rg;
    private String cpf;
    private DateTime dataCadastro;
    private Usuario usuario;
    private string email;
    private Endereco endereco;
    private IList relatos;
    private IList processos;

   

      
    #region Construtores
    public Cliente()
    {
    }

    public Cliente(string pIdCliente, string pNome, string pRG, string pCPF, DateTime pDataCadastro,
        Usuario pUsuario, string pEmails, Endereco pEndereco)
    {
        this.IdCliente = pIdCliente;
        this.Nome = pNome;
        this.Rg = pRG;
        this.Cpf = pCPF;
        this.DataCadastro = pDataCadastro;
        this.Usuario = pUsuario;
        this.Email = pEmails;
        this.Endereco = pEndereco;
    }
    #endregion

    #region Propriedades
    public string IdCliente {
      get { return idCliente; }
      set { idCliente = value; }
    }

    public String Nome {
      get { return nome; }
      set { nome = value; }
    }    

    public String Rg {
      get { return rg; }
      set { rg = value; }
    }

    public String Cpf {
      get { return cpf; }
      set { cpf = value; }
    }

    public DateTime DataCadastro {
      get { return dataCadastro; }
      set { dataCadastro = value; }
    }

    public Usuario Usuario
    {
        get { return usuario; }
        set { usuario = value; }
    }

    public string Email
    {
        get { return email; }
        set { email = value;}
    }

    public Endereco Endereco
    {
        get { return endereco;}
        set { endereco = value; }
    }
    

    public IList Relatos
    {
        get { return relatos; }
        set { relatos = value; }
    }

    public IList Processos
    {
        get { return processos; }
        set { processos = value; }
    }
    #endregion

    #region Metodos

    #region Persist
    public Boolean Persist()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Save(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Update
    public Boolean Update()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Update(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Delete
    public static Boolean Delete(string pIdCliente)
    {
        Cliente objCliente = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objCliente = (Cliente)objSession.Load(typeof(Model.Cliente), pIdCliente);
            objSession.Delete(objCliente);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region RetriveObject
    public static Cliente RetriveObject(string pIdCliente)
    {
        Cliente objCliente = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objCliente = (Cliente)objSession.Load(typeof(Model.Cliente), pIdCliente);
            objSession.Close();
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
        return objCliente;
    }
    #endregion

    #region RetriveObjects
    public static IList RetriveObjects()
    {
        IList listClientes;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            ICriteria criteria = objSession.CreateCriteria(typeof(Model.Cliente));
            criteria.AddOrder(Order.Asc("Nome"));

            listClientes = criteria.List();
            
            objSession.Close();
        }
        catch (Exception ex)
        {
            listClientes = null;
            objTransaction.Rollback();
            throw ex;
        }
        return listClientes;
    }
   

    public static IList RetriveObjects(string pNome)
    {
        IList listClientes;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            ICriteria criteria = objSession.CreateCriteria(typeof(Model.Cliente));
            criteria.Add(Expression.Like("Nome",pNome))
                    .AddOrder(Order.Asc("Nome"));
            listClientes = criteria.List();
            objSession.Close();

        }
        catch (Exception ex)
        {
            listClientes = null;
            objTransaction.Rollback();
            throw ex;
        }

        return listClientes;
    }
    #endregion

      #endregion

  }
}
